<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>node_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_node_test.js.html">node_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.nodeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.nodeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line22"></a>goog.require(&#39;goog.editor.node&#39;);
<a name="line23"></a>goog.require(&#39;goog.style&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.ExpectedFailures&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line28"></a>
<a name="line29"></a>var expectedFailures;
<a name="line30"></a>var parentNode;
<a name="line31"></a>var childNode1;
<a name="line32"></a>var childNode2;
<a name="line33"></a>var childNode3;
<a name="line34"></a>
<a name="line35"></a>var gChildWsNode1 = null;
<a name="line36"></a>var gChildTextNode1 = null;
<a name="line37"></a>var gChildNbspNode1 = null;
<a name="line38"></a>var gChildMixedNode1 = null;
<a name="line39"></a>var gChildWsNode2a = null;
<a name="line40"></a>var gChildWsNode2b = null;
<a name="line41"></a>var gChildTextNode3a = null;
<a name="line42"></a>var gChildWsNode3 = null;
<a name="line43"></a>var gChildTextNode3b = null;
<a name="line44"></a>
<a name="line45"></a>function setUpPage() {
<a name="line46"></a>  expectedFailures = new goog.testing.ExpectedFailures();
<a name="line47"></a>  parentNode = document.getElementById(&#39;parentNode&#39;);
<a name="line48"></a>  childNode1 = parentNode.childNodes[0];
<a name="line49"></a>  childNode2 = parentNode.childNodes[1];
<a name="line50"></a>  childNode3 = parentNode.childNodes[2];
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>function tearDown() {
<a name="line55"></a>  expectedFailures.handleTearDown();
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>function setUpDomTree() {
<a name="line59"></a>  gChildWsNode1 = document.createTextNode(&#39; \t\r\n&#39;);
<a name="line60"></a>  gChildTextNode1 = document.createTextNode(&#39;Child node&#39;);
<a name="line61"></a>  gChildNbspNode1 = document.createTextNode(&#39;\u00a0&#39;);
<a name="line62"></a>  gChildMixedNode1 = document.createTextNode(&#39;Text\n plus\u00a0&#39;);
<a name="line63"></a>  gChildWsNode2a = document.createTextNode(&#39;&#39;);
<a name="line64"></a>  gChildWsNode2b = document.createTextNode(&#39; &#39;);
<a name="line65"></a>  gChildTextNode3a = document.createTextNode(&#39;I am a grand child&#39;);
<a name="line66"></a>  gChildWsNode3 = document.createTextNode(&#39;   \t  \r   \n&#39;);
<a name="line67"></a>  gChildTextNode3b = document.createTextNode(&#39;I am also a grand child&#39;);
<a name="line68"></a>
<a name="line69"></a>  childNode3.appendChild(gChildTextNode3a);
<a name="line70"></a>  childNode3.appendChild(gChildWsNode3);
<a name="line71"></a>  childNode3.appendChild(gChildTextNode3b);
<a name="line72"></a>
<a name="line73"></a>  childNode1.appendChild(gChildMixedNode1);
<a name="line74"></a>  childNode1.appendChild(gChildWsNode1);
<a name="line75"></a>  childNode1.appendChild(gChildNbspNode1);
<a name="line76"></a>  childNode1.appendChild(gChildTextNode1);
<a name="line77"></a>
<a name="line78"></a>  childNode2.appendChild(gChildWsNode2a);
<a name="line79"></a>  childNode2.appendChild(gChildWsNode2b);
<a name="line80"></a>  document.body.appendChild(parentNode);
<a name="line81"></a>}
<a name="line82"></a>
<a name="line83"></a>function tearDownDomTree() {
<a name="line84"></a>  childNode1.innerHTML = childNode2.innerHTML = childNode3.innerHTML = &#39;&#39;;
<a name="line85"></a>  gChildWsNode1 = null;
<a name="line86"></a>  gChildTextNode1 = null;
<a name="line87"></a>  gChildNbspNode1 = null;
<a name="line88"></a>  gChildMixedNode1 = null;
<a name="line89"></a>  gChildWsNode2a = null;
<a name="line90"></a>  gChildWsNode2b = null;
<a name="line91"></a>  gChildTextNode3a = null;
<a name="line92"></a>  gChildWsNode3 = null;
<a name="line93"></a>  gChildTextNode3b = null;
<a name="line94"></a>}
<a name="line95"></a>
<a name="line96"></a>function testGetCompatModeQuirks() {
<a name="line97"></a>  var quirksIfr = document.createElement(&#39;iframe&#39;);
<a name="line98"></a>  document.body.appendChild(quirksIfr);
<a name="line99"></a>  // Webkit used to default to standards mode, but fixed this in
<a name="line100"></a>  // Safari 4/Chrome 2, aka, WebKit 530.
<a name="line101"></a>  expectedFailures.expectFailureFor(goog.userAgent.WEBKIT &amp;&amp;
<a name="line102"></a>                                    !goog.userAgent.isVersionOrHigher(&#39;530&#39;));
<a name="line103"></a>  expectedFailures.run(function() {
<a name="line104"></a>    assertFalse(&#39;Empty sourceless iframe is quirks mode, not standards mode&#39;,
<a name="line105"></a>        goog.editor.node.isStandardsMode(
<a name="line106"></a>            goog.dom.getFrameContentDocument(quirksIfr)));
<a name="line107"></a>  });
<a name="line108"></a>  document.body.removeChild(quirksIfr);
<a name="line109"></a>}
<a name="line110"></a>
<a name="line111"></a>function testGetCompatModeStandards() {
<a name="line112"></a>  var standardsIfr = document.createElement(&#39;iframe&#39;);
<a name="line113"></a>  document.body.appendChild(standardsIfr);
<a name="line114"></a>  var doc = goog.dom.getFrameContentDocument(standardsIfr);
<a name="line115"></a>  doc.open();
<a name="line116"></a>  doc.write(&#39;&lt;!DOCTYPE HTML&gt;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&amp;nbsp;&lt;/body&gt;&lt;/html&gt;&#39;);
<a name="line117"></a>  doc.close();
<a name="line118"></a>  assertTrue(&#39;Iframe with DOCTYPE written in is standards mode&#39;,
<a name="line119"></a>      goog.editor.node.isStandardsMode(doc));
<a name="line120"></a>  document.body.removeChild(standardsIfr);
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Creates a DOM tree and tests that getLeftMostLeaf returns proper node
<a name="line126"></a> */
<a name="line127"></a>function testGetLeftMostLeaf() {
<a name="line128"></a>  setUpDomTree();
<a name="line129"></a>
<a name="line130"></a>  assertEquals(&#39;Should skip ws node&#39;, gChildMixedNode1,
<a name="line131"></a>               goog.editor.node.getLeftMostLeaf(parentNode));
<a name="line132"></a>  assertEquals(&#39;Should skip ws node&#39;, gChildMixedNode1,
<a name="line133"></a>               goog.editor.node.getLeftMostLeaf(childNode1));
<a name="line134"></a>  assertEquals(&#39;Has no non ws leaves&#39;, childNode2,
<a name="line135"></a>               goog.editor.node.getLeftMostLeaf(childNode2));
<a name="line136"></a>  assertEquals(&#39;Should return first child&#39;, gChildTextNode3a,
<a name="line137"></a>               goog.editor.node.getLeftMostLeaf(childNode3));
<a name="line138"></a>  assertEquals(&#39;Has no children&#39;, gChildTextNode1,
<a name="line139"></a>               goog.editor.node.getLeftMostLeaf(gChildTextNode1));
<a name="line140"></a>
<a name="line141"></a>  tearDownDomTree();
<a name="line142"></a>}
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Creates a DOM tree and tests that getRightMostLeaf returns proper node
<a name="line147"></a> */
<a name="line148"></a>function testGetRightMostLeaf() {
<a name="line149"></a>  setUpDomTree();
<a name="line150"></a>
<a name="line151"></a>  assertEquals(&quot;Should return child3&#39;s rightmost child&quot;, gChildTextNode3b,
<a name="line152"></a>               goog.editor.node.getRightMostLeaf(parentNode));
<a name="line153"></a>  assertEquals(&#39;Should skip ws node&#39;, gChildTextNode1,
<a name="line154"></a>               goog.editor.node.getRightMostLeaf(childNode1));
<a name="line155"></a>  assertEquals(&#39;Has no non ws leaves&#39;, childNode2,
<a name="line156"></a>               goog.editor.node.getRightMostLeaf(childNode2));
<a name="line157"></a>  assertEquals(&#39;Should return last child&#39;, gChildTextNode3b,
<a name="line158"></a>               goog.editor.node.getRightMostLeaf(childNode3));
<a name="line159"></a>  assertEquals(&#39;Has no children&#39;, gChildTextNode1,
<a name="line160"></a>               goog.editor.node.getRightMostLeaf(gChildTextNode1));
<a name="line161"></a>
<a name="line162"></a>  tearDownDomTree();
<a name="line163"></a>}
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Creates a DOM tree and tests that getFirstChild properly ignores
<a name="line168"></a> * ignorable nodes
<a name="line169"></a> */
<a name="line170"></a>function testGetFirstChild() {
<a name="line171"></a>  setUpDomTree();
<a name="line172"></a>
<a name="line173"></a>  assertNull(&#39;Has no none ws children&#39;,
<a name="line174"></a>      goog.editor.node.getFirstChild(childNode2));
<a name="line175"></a>  assertEquals(&#39;Should skip first child, as it is ws&#39;, gChildMixedNode1,
<a name="line176"></a>      goog.editor.node.getFirstChild(childNode1));
<a name="line177"></a>  assertEquals(&#39;Should just return first child&#39;, gChildTextNode3a,
<a name="line178"></a>      goog.editor.node.getFirstChild(childNode3));
<a name="line179"></a>  assertEquals(&#39;Should return first child&#39;, childNode1,
<a name="line180"></a>      goog.editor.node.getFirstChild(parentNode));
<a name="line181"></a>
<a name="line182"></a>  assertNull(&#39;First child of a text node should return null&#39;,
<a name="line183"></a>      goog.editor.node.getFirstChild(gChildTextNode1));
<a name="line184"></a>  assertNull(&#39;First child of null should return null&#39;,
<a name="line185"></a>      goog.editor.node.getFirstChild(null));
<a name="line186"></a>
<a name="line187"></a>  tearDownDomTree();
<a name="line188"></a>}
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>/**
<a name="line192"></a> * Create a DOM tree and test that getLastChild properly ignores
<a name="line193"></a> * ignorable nodes
<a name="line194"></a> */
<a name="line195"></a>function testGetLastChild() {
<a name="line196"></a>  setUpDomTree();
<a name="line197"></a>
<a name="line198"></a>  assertNull(&#39;Has no none ws children&#39;,
<a name="line199"></a>      goog.editor.node.getLastChild(childNode2));
<a name="line200"></a>  assertEquals(&#39;Should skip last child, as it is ws&#39;, gChildTextNode1,
<a name="line201"></a>               goog.editor.node.getLastChild(childNode1));
<a name="line202"></a>  assertEquals(&#39;Should just return last child&#39;, gChildTextNode3b,
<a name="line203"></a>               goog.editor.node.getLastChild(childNode3));
<a name="line204"></a>  assertEquals(&#39;Should return last child&#39;, childNode3,
<a name="line205"></a>               goog.editor.node.getLastChild(parentNode));
<a name="line206"></a>
<a name="line207"></a>  assertNull(&#39;Last child of a text node should return null&#39;,
<a name="line208"></a>      goog.editor.node.getLastChild(gChildTextNode1));
<a name="line209"></a>  assertNull(&#39;Last child of null should return null&#39;,
<a name="line210"></a>      goog.editor.node.getLastChild(gChildTextNode1));
<a name="line211"></a>
<a name="line212"></a>  tearDownDomTree();
<a name="line213"></a>}
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>/**
<a name="line217"></a> * Test if nodes that should be ignorable return false and nodes that should
<a name="line218"></a> * not be ignored return true.
<a name="line219"></a> */
<a name="line220"></a>function testIsImportant() {
<a name="line221"></a>  var wsNode = document.createTextNode(&#39; \t\r\n&#39;);
<a name="line222"></a>  assertFalse(&#39;White space node is ignorable&#39;,
<a name="line223"></a>      goog.editor.node.isImportant(wsNode));
<a name="line224"></a>  var textNode = document.createTextNode(&#39;Hello&#39;);
<a name="line225"></a>  assertTrue(&#39;Text node is important&#39;, goog.editor.node.isImportant(textNode));
<a name="line226"></a>  var nbspNode = document.createTextNode(&#39;\u00a0&#39;);
<a name="line227"></a>  assertTrue(&#39;Node with nbsp is important&#39;,
<a name="line228"></a>      goog.editor.node.isImportant(nbspNode));
<a name="line229"></a>  var imageNode = document.createElement(&#39;img&#39;);
<a name="line230"></a>  assertTrue(&#39;Image node is important&#39;,
<a name="line231"></a>      goog.editor.node.isImportant(imageNode));
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * Test that isAllNonNbspWhiteSpace returns true if node contains only
<a name="line237"></a> * whitespace that is not nbsp and false otherwise
<a name="line238"></a> */
<a name="line239"></a>function testIsAllNonNbspWhiteSpace() {
<a name="line240"></a>  var wsNode = document.createTextNode(&#39; \t\r\n&#39;);
<a name="line241"></a>  assertTrue(&#39;String is all non nbsp&#39;,
<a name="line242"></a>      goog.editor.node.isAllNonNbspWhiteSpace(wsNode));
<a name="line243"></a>  var textNode = document.createTextNode(&#39;Hello&#39;);
<a name="line244"></a>  assertFalse(&#39;String should not be whitespace&#39;,
<a name="line245"></a>              goog.editor.node.isAllNonNbspWhiteSpace(textNode));
<a name="line246"></a>  var nbspNode = document.createTextNode(&#39;\u00a0&#39;);
<a name="line247"></a>  assertFalse(&#39;String has nbsp&#39;,
<a name="line248"></a>      goog.editor.node.isAllNonNbspWhiteSpace(nbspNode));
<a name="line249"></a>}
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/**
<a name="line253"></a> * Creates a DOM tree and Test that getPreviousSibling properly ignores
<a name="line254"></a> * ignorable nodes
<a name="line255"></a> */
<a name="line256"></a>function testGetPreviousSibling() {
<a name="line257"></a>  setUpDomTree();
<a name="line258"></a>
<a name="line259"></a>  assertNull(&#39;No previous sibling&#39;,
<a name="line260"></a>             goog.editor.node.getPreviousSibling(gChildTextNode3a));
<a name="line261"></a>  assertEquals(&#39;Should have text sibling&#39;, gChildTextNode3a,
<a name="line262"></a>               goog.editor.node.getPreviousSibling(gChildWsNode3));
<a name="line263"></a>  assertEquals(&#39;Should skip over white space sibling&#39;, gChildTextNode3a,
<a name="line264"></a>               goog.editor.node.getPreviousSibling(gChildTextNode3b));
<a name="line265"></a>  assertNull(&#39;No previous sibling&#39;,
<a name="line266"></a>             goog.editor.node.getPreviousSibling(gChildMixedNode1));
<a name="line267"></a>  assertEquals(&#39;Should have mixed text sibling&#39;, gChildMixedNode1,
<a name="line268"></a>               goog.editor.node.getPreviousSibling(gChildWsNode1));
<a name="line269"></a>  assertEquals(&#39;Should skip over white space sibling&#39;, gChildMixedNode1,
<a name="line270"></a>               goog.editor.node.getPreviousSibling(gChildNbspNode1));
<a name="line271"></a>  assertNotEquals(&#39;Should not move past ws and nbsp&#39;, gChildMixedNode1,
<a name="line272"></a>                  goog.editor.node.getPreviousSibling(gChildTextNode1));
<a name="line273"></a>  assertEquals(&#39;Should go to child 2&#39;, childNode2,
<a name="line274"></a>               goog.editor.node.getPreviousSibling(childNode3));
<a name="line275"></a>  assertEquals(&#39;Should go to child 1&#39;, childNode1,
<a name="line276"></a>               goog.editor.node.getPreviousSibling(childNode2));
<a name="line277"></a>  assertNull(&#39;Only has white space siblings&#39;,
<a name="line278"></a>             goog.editor.node.getPreviousSibling(gChildWsNode2b));
<a name="line279"></a>
<a name="line280"></a>  tearDownDomTree();
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a> * Creates a DOM tree and tests that getNextSibling properly ignores igrnorable
<a name="line286"></a> * nodes when determining the next sibling
<a name="line287"></a> */
<a name="line288"></a>function testGetNextSibling() {
<a name="line289"></a>  setUpDomTree();
<a name="line290"></a>
<a name="line291"></a>  assertEquals(&#39;Child 1 should have Child 2&#39;, childNode2,
<a name="line292"></a>               goog.editor.node.getNextSibling(childNode1));
<a name="line293"></a>  assertEquals(&#39;Child 2 should have child 3&#39;, childNode3,
<a name="line294"></a>               goog.editor.node.getNextSibling(childNode2));
<a name="line295"></a>  assertNull(&#39;Child 3 has no next sibling&#39;,
<a name="line296"></a>      goog.editor.node.getNextSibling(childNode3));
<a name="line297"></a>  assertNotEquals(&#39;Should not skip ws and nbsp nodes&#39;, gChildTextNode1,
<a name="line298"></a>                  goog.editor.node.getNextSibling(gChildMixedNode1));
<a name="line299"></a>  assertNotEquals(&#39;Should not skip nbsp node&#39;, gChildTextNode1,
<a name="line300"></a>                  goog.editor.node.getNextSibling(gChildWsNode1));
<a name="line301"></a>  assertEquals(&#39;Should have sibling&#39;, gChildTextNode1,
<a name="line302"></a>               goog.editor.node.getNextSibling(gChildNbspNode1));
<a name="line303"></a>  assertNull(&#39;Should have no next sibling&#39;,
<a name="line304"></a>             goog.editor.node.getNextSibling(gChildTextNode1));
<a name="line305"></a>  assertNull(&#39;Only has ws sibling&#39;,
<a name="line306"></a>      goog.editor.node.getNextSibling(gChildWsNode2a));
<a name="line307"></a>  assertNull(&#39;Has no next sibling&#39;,
<a name="line308"></a>      goog.editor.node.getNextSibling(gChildWsNode2b));
<a name="line309"></a>  assertEquals(&#39;Should skip ws node&#39;, gChildTextNode3b,
<a name="line310"></a>               goog.editor.node.getNextSibling(gChildTextNode3a));
<a name="line311"></a>
<a name="line312"></a>  tearDownDomTree();
<a name="line313"></a>}
<a name="line314"></a>
<a name="line315"></a>
<a name="line316"></a>function testIsEmpty() {
<a name="line317"></a>  var textNode = document.createTextNode(&#39;&#39;);
<a name="line318"></a>  assertTrue(&#39;Text node with no content should be empty&#39;,
<a name="line319"></a>      goog.editor.node.isEmpty(textNode));
<a name="line320"></a>  textNode.data = &#39;\xa0&#39;;
<a name="line321"></a>  assertTrue(&#39;Text node with nbsp should be empty&#39;,
<a name="line322"></a>      goog.editor.node.isEmpty(textNode));
<a name="line323"></a>  assertFalse(&#39;Text node with nbsp should not be empty when prohibited&#39;,
<a name="line324"></a>      goog.editor.node.isEmpty(textNode, true));
<a name="line325"></a>
<a name="line326"></a>  textNode.data = &#39;     &#39;;
<a name="line327"></a>  assertTrue(&#39;Text node with whitespace should be empty&#39;,
<a name="line328"></a>      goog.editor.node.isEmpty(textNode));
<a name="line329"></a>  textNode.data = &#39;notEmpty&#39;;
<a name="line330"></a>  assertFalse(&#39;Text node with text should not be empty&#39;,
<a name="line331"></a>      goog.editor.node.isEmpty(textNode));
<a name="line332"></a>
<a name="line333"></a>  var div = document.createElement(&#39;div&#39;);
<a name="line334"></a>  assertTrue(&#39;Empty div should be empty&#39;,
<a name="line335"></a>      goog.editor.node.isEmpty(div));
<a name="line336"></a>  div.innerHTML = &#39;&lt;iframe&gt;&lt;/iframe&gt;&#39;;
<a name="line337"></a>  assertFalse(&#39;Div containing an iframe is not empty&#39;,
<a name="line338"></a>      goog.editor.node.isEmpty(div));
<a name="line339"></a>  div.innerHTML = &#39;&lt;img&gt;&lt;/img&gt;&#39;;
<a name="line340"></a>  assertFalse(&#39;Div containing an image is not empty&#39;,
<a name="line341"></a>      goog.editor.node.isEmpty(div));
<a name="line342"></a>  div.innerHTML = &#39;&lt;embed&gt;&lt;/embed&gt;&#39;;
<a name="line343"></a>  assertFalse(&#39;Div containing an embed is not empty&#39;,
<a name="line344"></a>      goog.editor.node.isEmpty(div));
<a name="line345"></a>  div.innerHTML = &#39;&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;&#39;;
<a name="line346"></a>  assertTrue(&#39;Div containing other empty tags is empty&#39;,
<a name="line347"></a>      goog.editor.node.isEmpty(div));
<a name="line348"></a>  div.innerHTML = &#39;&lt;div&gt;&lt;span&gt;  &lt;/span&gt;&lt;/div&gt;&#39;;
<a name="line349"></a>  assertTrue(&#39;Div containing other empty tags and whitespace is empty&#39;,
<a name="line350"></a>      goog.editor.node.isEmpty(div));
<a name="line351"></a>  div.innerHTML = &#39;&lt;div&gt;&lt;span&gt;Not empty&lt;/span&gt;&lt;/div&gt;&#39;;
<a name="line352"></a>  assertFalse(&#39;Div containing tags and text is not empty&#39;,
<a name="line353"></a>      goog.editor.node.isEmpty(div));
<a name="line354"></a>
<a name="line355"></a>  var img = document.createElement(goog.dom.TagName.IMG);
<a name="line356"></a>  assertFalse(&#39;Empty img should not be empty&#39;,
<a name="line357"></a>      goog.editor.node.isEmpty(img));
<a name="line358"></a>
<a name="line359"></a>  var iframe = document.createElement(goog.dom.TagName.IFRAME);
<a name="line360"></a>  assertFalse(&#39;Empty iframe should not be empty&#39;,
<a name="line361"></a>      goog.editor.node.isEmpty(iframe));
<a name="line362"></a>
<a name="line363"></a>  var embed = document.createElement(&#39;embed&#39;);
<a name="line364"></a>  assertFalse(&#39;Empty embed should not be empty&#39;,
<a name="line365"></a>      goog.editor.node.isEmpty(embed));
<a name="line366"></a>}
<a name="line367"></a>
<a name="line368"></a>
<a name="line369"></a>/**
<a name="line370"></a> * Test that getLength returns 0 if the node has no length and no children,
<a name="line371"></a> * the # of children if the node has no length but does have children,
<a name="line372"></a> * and the length of the node if the node does have length
<a name="line373"></a> */
<a name="line374"></a>function testGetLength() {
<a name="line375"></a>  var parentNode = document.createElement(&#39;p&#39;);
<a name="line376"></a>
<a name="line377"></a>  assertEquals(&#39;Length 0 and no children&#39;, 0,
<a name="line378"></a>      goog.editor.node.getLength(parentNode));
<a name="line379"></a>
<a name="line380"></a>  var childNode1 = document.createTextNode(&#39;node 1&#39;);
<a name="line381"></a>  var childNode2 = document.createTextNode(&#39;node number 2&#39;);
<a name="line382"></a>  var childNode3 = document.createTextNode(&#39;&#39;);
<a name="line383"></a>  parentNode.appendChild(childNode1);
<a name="line384"></a>  parentNode.appendChild(childNode2);
<a name="line385"></a>  parentNode.appendChild(childNode3);
<a name="line386"></a>  assertEquals(&#39;Length 0 and 3 children&#39;, 3,
<a name="line387"></a>      goog.editor.node.getLength(parentNode));
<a name="line388"></a>  assertEquals(&#39;Text node, length 6&#39;, 6,
<a name="line389"></a>      goog.editor.node.getLength(childNode1));
<a name="line390"></a>  assertEquals(&#39;Text node, length 0&#39;, 0,
<a name="line391"></a>      goog.editor.node.getLength(childNode3));
<a name="line392"></a>}
<a name="line393"></a>
<a name="line394"></a>function testFindInChildrenSuccess() {
<a name="line395"></a>  var parentNode = document.createElement(&#39;div&#39;);
<a name="line396"></a>  parentNode.innerHTML = &#39;&lt;div&gt;foo&lt;/div&gt;&lt;b&gt;foo2&lt;/b&gt;&#39;;
<a name="line397"></a>
<a name="line398"></a>  var index = goog.editor.node.findInChildren(parentNode,
<a name="line399"></a>      function(node) {
<a name="line400"></a>        return node.tagName == &#39;B&#39;;
<a name="line401"></a>      });
<a name="line402"></a>  assertEquals(&#39;Should find second child&#39;, index, 1);
<a name="line403"></a>}
<a name="line404"></a>
<a name="line405"></a>function testFindInChildrenFailure() {
<a name="line406"></a>  var parentNode = document.createElement(&#39;div&#39;);
<a name="line407"></a>  parentNode.innerHTML = &#39;&lt;div&gt;foo&lt;/div&gt;&lt;b&gt;foo2&lt;/b&gt;&#39;;
<a name="line408"></a>
<a name="line409"></a>  var index = goog.editor.node.findInChildren(parentNode,
<a name="line410"></a>      function(node) {
<a name="line411"></a>        return false;
<a name="line412"></a>      });
<a name="line413"></a>  assertNull(&quot;Shouldn&#39;t find a child&quot;, index);
<a name="line414"></a>}
<a name="line415"></a>
<a name="line416"></a>function testFindHighestMatchingAncestor() {
<a name="line417"></a>  setUpDomTree();
<a name="line418"></a>  var predicateFunc = function(node) {
<a name="line419"></a>    return node.tagName == &#39;DIV&#39;;
<a name="line420"></a>  };
<a name="line421"></a>  var node = goog.editor.node.findHighestMatchingAncestor(
<a name="line422"></a>      gChildTextNode3a, predicateFunc);
<a name="line423"></a>  assertNotNull(&#39;Should return an ancestor&#39;, node);
<a name="line424"></a>  assertEquals(&#39;Should have found &quot;parentNode&quot; as the last &#39; +
<a name="line425"></a>               &#39;ancestor matching the predicate&#39;,
<a name="line426"></a>               parentNode,
<a name="line427"></a>               node);
<a name="line428"></a>
<a name="line429"></a>  predicateFunc = function(node) {
<a name="line430"></a>    return node.childNodes.length == 1;
<a name="line431"></a>  };
<a name="line432"></a>  node = goog.editor.node.findHighestMatchingAncestor(gChildTextNode3a,
<a name="line433"></a>                                                      predicateFunc);
<a name="line434"></a>  assertNull(&quot;Shouldn&#39;t return an ancestor&quot;, node);
<a name="line435"></a>
<a name="line436"></a>  tearDownDomTree();
<a name="line437"></a>}
<a name="line438"></a>
<a name="line439"></a>function testIsBlock() {
<a name="line440"></a>  var blockDisplays = [&#39;block&#39;, &#39;list-item&#39;, &#39;table&#39;, &#39;table-caption&#39;,
<a name="line441"></a>    &#39;table-cell&#39;, &#39;table-column&#39;, &#39;table-column-group&#39;, &#39;table-footer&#39;,
<a name="line442"></a>    &#39;table-footer-group&#39;, &#39;table-header-group&#39;, &#39;table-row&#39;,
<a name="line443"></a>    &#39;table-row-group&#39;];
<a name="line444"></a>
<a name="line445"></a>  var structuralTags = [
<a name="line446"></a>    goog.dom.TagName.BODY,
<a name="line447"></a>    goog.dom.TagName.FRAME,
<a name="line448"></a>    goog.dom.TagName.FRAMESET,
<a name="line449"></a>    goog.dom.TagName.HEAD,
<a name="line450"></a>    goog.dom.TagName.HTML
<a name="line451"></a>  ];
<a name="line452"></a>
<a name="line453"></a>  // The following tags are considered inline in IE, except LEGEND which is
<a name="line454"></a>  // only a block element in WEBKIT.
<a name="line455"></a>  var ambiguousTags = [
<a name="line456"></a>    goog.dom.TagName.DETAILS,
<a name="line457"></a>    goog.dom.TagName.HR,
<a name="line458"></a>    goog.dom.TagName.ISINDEX,
<a name="line459"></a>    goog.dom.TagName.LEGEND,
<a name="line460"></a>    goog.dom.TagName.MAP,
<a name="line461"></a>    goog.dom.TagName.NOFRAMES,
<a name="line462"></a>    goog.dom.TagName.OPTGROUP,
<a name="line463"></a>    goog.dom.TagName.OPTION,
<a name="line464"></a>    goog.dom.TagName.SUMMARY
<a name="line465"></a>  ];
<a name="line466"></a>
<a name="line467"></a>  // Older versions of IE and Gecko consider the following elements to be
<a name="line468"></a>  // inline, but IE9+ and Gecko 2.0+ recognize the new elements.
<a name="line469"></a>  var legacyAmbiguousTags = [
<a name="line470"></a>    goog.dom.TagName.ARTICLE,
<a name="line471"></a>    goog.dom.TagName.ASIDE,
<a name="line472"></a>    goog.dom.TagName.FIGCAPTION,
<a name="line473"></a>    goog.dom.TagName.FIGURE,
<a name="line474"></a>    goog.dom.TagName.FOOTER,
<a name="line475"></a>    goog.dom.TagName.HEADER,
<a name="line476"></a>    goog.dom.TagName.HGROUP,
<a name="line477"></a>    goog.dom.TagName.NAV,
<a name="line478"></a>    goog.dom.TagName.SECTION
<a name="line479"></a>  ];
<a name="line480"></a>
<a name="line481"></a>  var tagsToIgnore = goog.array.flatten(structuralTags, ambiguousTags);
<a name="line482"></a>
<a name="line483"></a>  if ((goog.userAgent.IE &amp;&amp; !goog.userAgent.isDocumentModeOrHigher(9)) ||
<a name="line484"></a>      (goog.userAgent.GECKO &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;2&#39;))) {
<a name="line485"></a>    goog.array.extend(tagsToIgnore, legacyAmbiguousTags);
<a name="line486"></a>  }
<a name="line487"></a>
<a name="line488"></a>  // Appending an applet tag can cause the test to hang if Java is blocked on
<a name="line489"></a>  // the system.
<a name="line490"></a>  tagsToIgnore.push(goog.dom.TagName.APPLET);
<a name="line491"></a>
<a name="line492"></a>  // Appending an embed tag to the page in IE brings up a warning dialog about
<a name="line493"></a>  // loading Java content.
<a name="line494"></a>  if (goog.userAgent.IE) {
<a name="line495"></a>    tagsToIgnore.push(goog.dom.TagName.EMBED);
<a name="line496"></a>  }
<a name="line497"></a>
<a name="line498"></a>  for (var tag in goog.dom.TagName) {
<a name="line499"></a>    if (goog.array.contains(tagsToIgnore, tag)) {
<a name="line500"></a>      continue;
<a name="line501"></a>    }
<a name="line502"></a>
<a name="line503"></a>    var el = goog.dom.createElement(tag);
<a name="line504"></a>    document.body.appendChild(el);
<a name="line505"></a>    var display = goog.style.getCascadedStyle(el, &#39;display&#39;) ||
<a name="line506"></a>                  goog.style.getComputedStyle(el, &#39;display&#39;);
<a name="line507"></a>    goog.dom.removeNode(el);
<a name="line508"></a>
<a name="line509"></a>    if (goog.editor.node.isBlockTag(el)) {
<a name="line510"></a>      assertContains(&#39;Display for &#39; + tag + &#39; should be block-like&#39;,
<a name="line511"></a>          display, blockDisplays);
<a name="line512"></a>    } else {
<a name="line513"></a>      assertNotContains(&#39;Display for &#39; + tag + &#39; should not be block-like&#39;,
<a name="line514"></a>          display, blockDisplays);
<a name="line515"></a>    }
<a name="line516"></a>  }
<a name="line517"></a>}
<a name="line518"></a>
<a name="line519"></a>function createDivWithTextNodes(var_args) {
<a name="line520"></a>  var dom = goog.dom.createDom(&#39;div&#39;);
<a name="line521"></a>  for (var i = 0; i &lt; arguments.length; i++) {
<a name="line522"></a>    goog.dom.appendChild(dom, goog.dom.createTextNode(arguments[i]));
<a name="line523"></a>  }
<a name="line524"></a>  return dom;
<a name="line525"></a>}
<a name="line526"></a>
<a name="line527"></a>function testSkipEmptyTextNodes() {
<a name="line528"></a>  assertNull(&#39;skipEmptyTextNodes should gracefully handle null&#39;,
<a name="line529"></a>      goog.editor.node.skipEmptyTextNodes(null));
<a name="line530"></a>
<a name="line531"></a>  var dom1 = createDivWithTextNodes(&#39;abc&#39;, &#39;&#39;, &#39;xyz&#39;, &#39;&#39;, &#39;&#39;);
<a name="line532"></a>  assertEquals(&#39;expected not to skip first child&#39;, dom1.firstChild,
<a name="line533"></a>      goog.editor.node.skipEmptyTextNodes(dom1.firstChild));
<a name="line534"></a>  assertEquals(&#39;expected to skip second child&#39;, dom1.childNodes[2],
<a name="line535"></a>      goog.editor.node.skipEmptyTextNodes(dom1.childNodes[1]));
<a name="line536"></a>  assertNull(&#39;expected to skip all the rest of the children&#39;,
<a name="line537"></a>      goog.editor.node.skipEmptyTextNodes(dom1.childNodes[3]));
<a name="line538"></a>}
<a name="line539"></a>
<a name="line540"></a>function testIsEditableContainer() {
<a name="line541"></a>  var editableContainerElement = document.getElementById(&#39;editableTest&#39;);
<a name="line542"></a>  assertTrue(&#39;Container element should be considered editable container&#39;,
<a name="line543"></a>      goog.editor.node.isEditableContainer(editableContainerElement));
<a name="line544"></a>
<a name="line545"></a>  var nonEditableContainerElement = document.getElementById(&#39;parentNode&#39;);
<a name="line546"></a>  assertFalse(&#39;Other element should not be considered editable container&#39;,
<a name="line547"></a>      goog.editor.node.isEditableContainer(nonEditableContainerElement));
<a name="line548"></a>}
<a name="line549"></a>
<a name="line550"></a>function testIsEditable() {
<a name="line551"></a>  var editableContainerElement = document.getElementById(&#39;editableTest&#39;);
<a name="line552"></a>  var childNode = editableContainerElement.firstChild;
<a name="line553"></a>  var childElement = editableContainerElement.getElementsByTagName(&#39;span&#39;)[0];
<a name="line554"></a>
<a name="line555"></a>  assertFalse(&#39;Container element should not be considered editable&#39;,
<a name="line556"></a>      goog.editor.node.isEditable(editableContainerElement));
<a name="line557"></a>  assertTrue(&#39;Child text node should be considered editable&#39;,
<a name="line558"></a>      goog.editor.node.isEditable(childNode));
<a name="line559"></a>  assertTrue(&#39;Child element should be considered editable&#39;,
<a name="line560"></a>      goog.editor.node.isEditable(childElement));
<a name="line561"></a>  assertTrue(&#39;Grandchild node should be considered editable&#39;,
<a name="line562"></a>      goog.editor.node.isEditable(childElement.firstChild));
<a name="line563"></a>  assertFalse(&#39;Other element should not be considered editable&#39;,
<a name="line564"></a>      goog.editor.node.isEditable(document.getElementById(&#39;parentNode&#39;)));
<a name="line565"></a>}
<a name="line566"></a>
<a name="line567"></a>function testFindTopMostEditableAncestor() {
<a name="line568"></a>  var root = document.getElementById(&#39;editableTest&#39;);
<a name="line569"></a>  var span = root.getElementsByTagName(goog.dom.TagName.SPAN)[0];
<a name="line570"></a>  var textNode = span.firstChild;
<a name="line571"></a>
<a name="line572"></a>  assertEquals(&#39;Should return self if self is matched.&#39;,
<a name="line573"></a>      textNode, goog.editor.node.findTopMostEditableAncestor(textNode,
<a name="line574"></a>      function(node) {
<a name="line575"></a>        return node.nodeType == goog.dom.NodeType.TEXT;
<a name="line576"></a>      }));
<a name="line577"></a>  assertEquals(&#39;Should not walk out of editable node.&#39;,
<a name="line578"></a>      null, goog.editor.node.findTopMostEditableAncestor(textNode,
<a name="line579"></a>      function(node) {
<a name="line580"></a>        return node.tagName == goog.dom.TagName.BODY;
<a name="line581"></a>      }));
<a name="line582"></a>  assertEquals(&#39;Should not match editable container.&#39;,
<a name="line583"></a>      null, goog.editor.node.findTopMostEditableAncestor(textNode,
<a name="line584"></a>      function(node) {
<a name="line585"></a>        return node.tagName == goog.dom.TagName.DIV;
<a name="line586"></a>      }));
<a name="line587"></a>  assertEquals(&#39;Should find node in editable container.&#39;,
<a name="line588"></a>      span, goog.editor.node.findTopMostEditableAncestor(textNode,
<a name="line589"></a>      function(node) {
<a name="line590"></a>        return node.tagName == goog.dom.TagName.SPAN;
<a name="line591"></a>      }));
<a name="line592"></a>}
<a name="line593"></a>
<a name="line594"></a>function testSplitDomTreeAt() {
<a name="line595"></a>  var innerHTML = &#39;&lt;p&gt;1&lt;b&gt;2&lt;/b&gt;3&lt;/p&gt;&#39;;
<a name="line596"></a>  var root = goog.dom.createElement(goog.dom.TagName.DIV);
<a name="line597"></a>
<a name="line598"></a>  root.innerHTML = innerHTML;
<a name="line599"></a>  var result = goog.editor.node.splitDomTreeAt(
<a name="line600"></a>      root.getElementsByTagName(goog.dom.TagName.B)[0], null, root);
<a name="line601"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;p&gt;1&lt;b&gt;2&lt;/b&gt;&lt;/p&gt;&#39;, root);
<a name="line602"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;p&gt;3&lt;/p&gt;&#39;, result);
<a name="line603"></a>
<a name="line604"></a>  root.innerHTML = innerHTML;
<a name="line605"></a>  result = goog.editor.node.splitDomTreeAt(
<a name="line606"></a>      root.getElementsByTagName(goog.dom.TagName.B)[0],
<a name="line607"></a>      goog.dom.createTextNode(&#39;and&#39;),
<a name="line608"></a>      root);
<a name="line609"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;p&gt;1&lt;b&gt;2&lt;/b&gt;&lt;/p&gt;&#39;, root);
<a name="line610"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;&lt;p&gt;and3&lt;/p&gt;&#39;, result);
<a name="line611"></a>}
<a name="line612"></a>
<a name="line613"></a>function testTransferChildren() {
<a name="line614"></a>  var prefix = &#39;&lt;b&gt;Bold 1&lt;/b&gt;&#39;;
<a name="line615"></a>  var innerHTML = &#39;&lt;b&gt;Bold&lt;/b&gt;&lt;ul&gt;&lt;li&gt;Item 1&lt;/li&gt;&lt;li&gt;Item 2&lt;/li&gt;&lt;/ul&gt;&#39;;
<a name="line616"></a>
<a name="line617"></a>  var root1 = goog.dom.createElement(goog.dom.TagName.DIV);
<a name="line618"></a>  root1.innerHTML = innerHTML;
<a name="line619"></a>
<a name="line620"></a>  var root2 = goog.dom.createElement(goog.dom.TagName.P);
<a name="line621"></a>  root2.innerHTML = prefix;
<a name="line622"></a>
<a name="line623"></a>  var b = root1.getElementsByTagName(goog.dom.TagName.B)[0];
<a name="line624"></a>
<a name="line625"></a>  // Transfer the children.
<a name="line626"></a>  goog.editor.node.transferChildren(root2, root1);
<a name="line627"></a>  assertEquals(0, root1.childNodes.length);
<a name="line628"></a>  goog.testing.dom.assertHtmlContentsMatch(prefix + innerHTML, root2);
<a name="line629"></a>  assertEquals(b, root2.getElementsByTagName(goog.dom.TagName.B)[1]);
<a name="line630"></a>
<a name="line631"></a>  // Transfer them back.
<a name="line632"></a>  goog.editor.node.transferChildren(root1, root2);
<a name="line633"></a>  assertEquals(0, root2.childNodes.length);
<a name="line634"></a>  goog.testing.dom.assertHtmlContentsMatch(prefix + innerHTML, root1);
<a name="line635"></a>  assertEquals(b, root1.getElementsByTagName(goog.dom.TagName.B)[1]);
<a name="line636"></a>}
</pre>


</body>
</html>
